/*
Auhtor      : Pratibh Prakash
Company     : Fujitsu America
Created Date: 14 Sep-2009
Modified    : 
Description : Test class for  maintainOpportunityForecastObject trigger.
*/
@isTest
private class maintainOpportunityForecastObjectTest {

    static testMethod void testOpportuniytForecastTrigger() {
       
		//Preparing mock data for test
		Account testAccount = new Account(Name='Test Account');
		insert testAccount;
		Opportunity testOpp = new Opportunity(Name = 'Test Opportunity', AccountId = testAccount.Id, StageName = '4 - Negotiate', IM_Booked_Date__c = Date.Today(), closedate = Date.Today()+1);
		insert testOpp;
		
		//Retrieving standard pricebook
		Pricebook2 testStandardPriceBook = [select id from Pricebook2 where IsStandard = true];
		//Creating a Prooduct, PriceBookEntry for it and using these record to generate OpportunityLineItem record.
       	Product2 testProd = new Product2(Name = 'Test Product', IsActive= true, CanUseRevenueSchedule = true, NumberOfRevenueInstallments=12, RevenueInstallmentPeriod='Monthly', RevenueScheduleType='Divide');
		insert testProd;
      	PriceBookEntry testPriceBookEntry =  new PriceBookEntry(Product2Id = testProd.Id, Pricebook2Id = testStandardPriceBook.Id, UnitPrice = 1, IsActive = true, UseStandardPrice = false);
		insert testPriceBookEntry;
		
		OpportunityLineItem oppProd = new OpportunityLineItem(Quantity = 12, PricebookEntryId = testPriceBookEntry.Id, OpportunityId = testOpp.Id, unitPrice = 1, Update_New_Revenue_Forecast__c =false );
		insert oppProd;//insert event of Trigger is called.
		
		//Preparing Schedules for OpportunityLineItems
		List<OpportunityLineItemSchedule> oppScheduleList = new List<OpportunityLineItemSchedule>();
		for(Integer i=0; i<=5; i++){
			OpportunityLineItemSchedule olis = new OpportunityLineItemSchedule(OpportunityLineItemId = oppProd.Id, Revenue=i+1, ScheduleDate= Date.Today()+(i*30), Type='Revenue');
			oppScheduleList.add(olis);
		} 
		insert oppScheduleList;
		
		//Craeting Opportunity Forecast record and relating it to OpportunityLineItem created above. This record is deleted in trigger when OpportunityLineiIteamis updated. 
		IM_Opportunity_Forecast__c oppFcst = new IM_Opportunity_Forecast__c();
		oppFcst.OpportunityLIneItemScheduleId__c = oppScheduleList[0].Id;
		oppFcst.Name = 'Test';
		oppFcst.Opportunity__c = testOpp.Id;
		oppFcst.Product__c = testProd.Id;
		oppFcst.OpportunityLineItemId__c = oppProd.Id;
		oppFcst.Revenue__c = 10;
		oppFcst.Schedule_Date__c = Date.Today();
		insert oppFcst;
		
		//updating OpportunityLineItem record
		oppProd.Update_New_Revenue_Forecast__c = true;
		update oppProd;//Update event of trigger is called
		
		//deleting OpportunityLineItem record
		delete oppProd;//delete event of trigger is called.
		
	}
}